# Maintainer: Mario Emmenlauer <memmenlauer@biodataanalysis.de>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=protobuf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.12.3
_gtest_ver=1.10.0
pkgrel=1
pkgdesc="Protocol Buffers - Google's data interchange format (mingw-w64)"
arch=('any')
url='https://developers.google.com/protocol-buffers/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "automake" "autoconf" "libtool" "unzip")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/google/protobuf/archive/v${pkgver}.tar.gz
        googletest-release-${_gtest_ver}.tar.gz::https://github.com/google/googletest/archive/release-${_gtest_ver}.tar.gz
        0001-protobuf-3.1.0-gcc6.2.0-tests.patch
        0002-windres-invocation.patch)
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('71030a04aedf9f612d2991c1c552317038c3c5a2b578ac4745267a45e7037c29'
            '9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb'
            '15c2248597356040be0111d5bfc7317d59a49552d5cd05b0fa70c76980b7ca66'
            '7c5434ca428784058d6a3bb4a14ff8ff2c4ee843c51800751bbee599d5a63288')

prepare() {
  tar -xzf "${srcdir}/${_realname}-${pkgver}.tar.gz"
  cd "${srcdir}/${_realname}-${pkgver}"

  cp -r ../googletest-release-${_gtest_ver}/googlemock third_party/googletest/googlemock
  cp -r ../googletest-release-${_gtest_ver}/googletest third_party/googletest/googletest

  patch -p1 -i ${srcdir}/0001-protobuf-3.1.0-gcc6.2.0-tests.patch
  patch -p1 -i ${srcdir}/0002-windres-invocation.patch
  ./autogen.sh
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  cp -rf "${_realname}-${pkgver}" "build-${MINGW_CHOST}"
  
  # remove googlemock and googletest directories to allow both architectures to prepare correctly
  rm -rf "${srcdir}/${_realname}-${pkgver}/googlemock"
  rm -rf "${srcdir}/${_realname}-${pkgver}/googletest"
  
  cd "${srcdir}/build-${MINGW_CHOST}"

  # autogen and configure are used to generate the pkg-config files
  # that will be manually installed. The cmake-based build does not
  # create pkg-config files. Remove autogen and configure if pkg-config
  # is not required:
  ./configure \
      --prefix="${MINGW_PREFIX}" \
      --build="${MINGW_CHOST}" \
      --host="${MINGW_CHOST}" \
      --enable-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCMAKE_SKIP_RPATH="ON" \
      -Dprotobuf_MSVC_STATIC_RUNTIME="ON" \
      -DBUILD_SHARED_LIBS="ON" \
      -Dprotobuf_BUILD_TESTS="OFF" \
      -Dprotobuf_WITH_ZLIB="ON" \
      cmake

  make -j1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}/${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/build-${MINGW_CHOST}/protobuf.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/protobuf.pc"
  install -Dm644 "${srcdir}/build-${MINGW_CHOST}/protobuf-lite.pc" "${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/protobuf-lite.pc"
}
